package net.lab1024.sa.admin.module.business.purchase.supplier.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.lab1024.sa.admin.constant.AdminSwaggerTagConst;
import net.lab1024.sa.admin.module.business.purchase.supplier.domain.entity.SupplierEntity;
import net.lab1024.sa.admin.module.business.purchase.supplier.domain.form.SupplierAddForm;
import net.lab1024.sa.admin.module.business.purchase.supplier.domain.form.SupplierQueryForm;
import net.lab1024.sa.admin.module.business.purchase.supplier.domain.form.SupplierUpdateForm;
import net.lab1024.sa.admin.module.business.purchase.supplier.domain.vo.SupplierVO;
import net.lab1024.sa.admin.module.business.purchase.supplier.service.SupplierService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 供应商管理Controller
 *
 * @Author 1024创新实验室
 * @Date 2024/07/19 20:30:00
 * @Wechat zhuoda1024
 * @Email lab1024@163.com
 * @Copyright <a href="https://1024lab.net">1024创新实验室</a>
 */
@RestController
@Tag(name = AdminSwaggerTagConst.Business.PURCHASE_SUPPLIER)
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    @Operation(summary = "分页查询供应商列表")
    @PostMapping("/purchase/supplier/page")
    @SaCheckPermission("purchase:supplier:query")
    public ResponseDTO<PageResult<SupplierVO>> queryPage(@RequestBody @Valid SupplierQueryForm queryForm) {
        return supplierService.queryPage(queryForm);
    }

    @Operation(summary = "添加供应商")
    @PostMapping("/purchase/supplier/add")
    @SaCheckPermission("purchase:supplier:add")
    public ResponseDTO<String> add(@RequestBody @Valid SupplierAddForm addForm) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        return supplierService.add(addForm, requestUser.getUserId(), requestUser.getUserName());
    }

    @Operation(summary = "更新供应商")
    @PostMapping("/purchase/supplier/update")
    @SaCheckPermission("purchase:supplier:update")
    public ResponseDTO<String> update(@RequestBody @Valid SupplierUpdateForm updateForm) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        return supplierService.update(updateForm, requestUser.getUserId(), requestUser.getUserName());
    }

    @Operation(summary = "删除供应商")
    @GetMapping("/purchase/supplier/delete/{supplierId}")
    @SaCheckPermission("purchase:supplier:delete")
    public ResponseDTO<String> delete(@PathVariable Long supplierId) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        return supplierService.delete(supplierId, requestUser.getUserId(), requestUser.getUserName());
    }

    @Operation(summary = "查询供应商详情")
    @GetMapping("/purchase/supplier/{supplierId}")
    @SaCheckPermission("purchase:supplier:detail")
    public ResponseDTO<SupplierVO> queryDetail(@PathVariable Long supplierId) {
        return supplierService.queryDetail(supplierId);
    }

    @Operation(summary = "启用/禁用供应商")
    @PostMapping("/purchase/supplier/updateStatus")
    @SaCheckPermission("purchase:supplier:updateStatus")
    public ResponseDTO<String> updateStatus(@RequestParam Long supplierId, @RequestParam Integer status) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        return supplierService.updateStatus(supplierId, status, requestUser.getUserId(), requestUser.getUserName());
    }

    @Operation(summary = "获取所有启用的供应商列表")
    @GetMapping("/purchase/supplier/enabled/list")
    public ResponseDTO<List<SupplierEntity>> queryEnabledList() {
        return supplierService.queryEnabledList();
    }
}